package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.modules.archives.entity.UserExpertise;
import com.lantosec.hmrp.modules.archives.mapper.UserExpertiseMapper;
import com.lantosec.hmrp.modules.archives.service.IUserExpertiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserExpertiseVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 专业技术职务信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-07-13
 */
@Service
@Primary
@Log4j2
public class UserExpertiseServiceImpl extends ServiceImpl<UserExpertiseMapper, UserExpertise> implements IUserExpertiseService {
    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public List<UserExpertiseVO> getAll(String userId) {
        List<UserExpertiseVO> userExpertiseVOS = new ArrayList<>();
        List<UserExpertise> userExpertise = this.list(new QueryWrapper<UserExpertise>().eq(UserExpertise.ColName.user_id.toString(),userId));
        if(CollectionUtils.isNotEmpty(userExpertise)){
            userExpertise.forEach(param->{
                UserExpertiseVO userExpertiseVO = param.convertToVO();
                //返回附件路径
                String photos = param.getPhotos();
                if(StringUtils.isNotEmpty(photos)){
                    String[] photo = photos.split(",");
                    List<String> list = new ArrayList<>();
                    for(String s:photo){
                        SysUploads sysUploads = iSysUploadsService.getById(s);
                        String savePath = sysUploads.getSavePath();
                        String fileName = sysUploads.getFileName();
                        list.add(InitDataListener.uploadsUrl + savePath + fileName);
                    }
                    userExpertiseVO.setSavePath(list);
                }
                userExpertiseVOS.add(userExpertiseVO);
            });
        }
        return userExpertiseVOS;
    }
}
